//
//  AssetLayout.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/5/4.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class AssetLayout: UICollectionViewFlowLayout {
    fileprivate var attributesArr: [UICollectionViewLayoutAttributes] = []
    var contenSizeHeight:CGFloat = 0.00
    var firstBool = false
    
    var selectIndex: Int = 1
    
    override init() {
        super.init()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func prepare() {
        super.prepare()
        itemSize = CGSize(width:kSCREEN_WIDTH-46 , height: 163)
        self.minimumLineSpacing = 5.0
        let  insert = ((self.collectionView?.frame.size.width)!-self.itemSize.width)/2
        self.sectionInset = UIEdgeInsetsMake(0, insert, 0, insert);
    
        self.scrollDirection = UICollectionViewScrollDirection.horizontal
        let itemsCount = collectionView?.numberOfItems(inSection: 0) ?? 0
        for itemIndex in 0..<itemsCount {
            let indexPath = IndexPath(item: itemIndex, section: 0)
            let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
            // 把每一个新的属性保存起来
            attributesArr.append(attributes)
        }
        if !firstBool {
            self.collectionView?.contentOffset = CGPoint(x: selectIndex*Int(kSCREEN_WIDTH-41), y: 0)
            firstBool = true
        }
    }
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        let array = super.layoutAttributesForElements(in: rect)
        let centetX = (self.collectionView?.contentOffset.x)! + (self.collectionView?.frame.size.width)!/2
        for attrs:UICollectionViewLayoutAttributes in array! {
            let delta = abs(attrs.center.x - centetX)
            let scale = 1 - delta/(self.collectionView?.frame.size.width)!;
            if scale < 0.95 {
                attrs.transform = CGAffineTransform(scaleX: 0.95, y: 0.95)
            }else {
                attrs.transform = CGAffineTransform(scaleX: scale, y: scale)
            }
        }
        
        return array
    }

    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        let attributes = super.layoutAttributesForItem(at: indexPath)
        attributes?.transform = CGAffineTransform(scaleX: 0.95, y: 0.95)
        return attributes
    }
    
    override open func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
        var rect = CGRect();
        rect.origin.x = proposedContentOffset.x
        rect.origin.y = 0
        rect.size = (self.collectionView?.frame.size)!
        let array = super.layoutAttributesForElements(in: rect)
        let centerX = (self.collectionView?.frame.size.width)!/2 + proposedContentOffset.x
        
        var minDelta = MAXFLOAT
        
        for attrs:UICollectionViewLayoutAttributes in array! {
            if CGFloat(fabs(minDelta))  > CGFloat(fabs(attrs.center.x-centerX)) {
                minDelta = Float(attrs.center.x-centerX)
            }
            
        }
        let point:CGPoint = CGPoint(x: proposedContentOffset.x + CGFloat(minDelta) , y: proposedContentOffset.y)
       
        return point
        
        
    }
    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
        
        return true
    }
}
